//
//  EmoticonTextAttachment.swift
//  EmojiKeyboard
//
//  Created by ZhangZiang on 15/12/28.
//  Copyright © 2015年 ZhangZiang. All rights reserved.
//

import UIKit

class EmoticonTextAttachment: NSTextAttachment {

    var chs : String?
    
    func emoticonTextToImageText(em : Emoticon, font : UIFont) -> NSAttributedString {
                   
            //到这里则是新浪表情
            let image = UIImage(contentsOfFile: em.imagePath ?? "")
            //设置附件属性
            let attachment = EmoticonTextAttachment()
            
            attachment.image = image
            attachment.chs = em.chs
            //调整附件的大小
            let lineHeight = font.lineHeight ?? 0
            
            attachment.bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
            //设置富文本
            let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
            //需要给富文本设置自己的文字属性
            imageText.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        
            return imageText
        
        }

    }
